package net.mihome.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.widget.LoadingDialog;

import org.apache.http.Header;

import java.util.Map;

/**
 * Created by ThinKPad on 2015/1/8.
 */
public class UpdateUserActivity  extends  BaseActivity{
    private TextView textViewTitle;
    private TextView textViewName;
    private EditText editTextValue;
    private String item_name;
    private String show_name;
    private View backBtn;
    private View okBtn;
    private LoadingDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_user);
        this.progressDialog=new LoadingDialog(this);
        this.progressDialog.setContentView(R.layout.loadingdialog);
        textViewTitle= (TextView) findViewById(R.id.update_user_title);
        textViewName= (TextView) findViewById(R.id.update_user_show_name);
        editTextValue= (EditText) findViewById(R.id.update_user_item_value);
        backBtn=findViewById(R.id.update_user_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        okBtn=findViewById(R.id.update_user_ok);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String value=editTextValue.getText().toString();
                if(value==null||"".equals(value.trim())){
                    UIHelper.showAlertDialog(UpdateUserActivity.this,"提示","请输入"+show_name);
                }else{
                    updateUser(item_name,value);
                }
            }
        });
        //
        Intent intent=getIntent();
        String title=intent.getStringExtra("title");
        item_name=intent.getStringExtra("item_name");
        show_name=intent.getStringExtra("show_name");
        textViewTitle.setText(title);
        textViewName.setText(show_name);
    }
    /**
     * 修改用户信息
     * */
    public void updateUser(String name,String value){
        if(!this.progressDialog.isShowing())
        {
            progressDialog.show();
        }
        AppContext.getInstance().updateUser(name, value, new MiAsyncHttpResponseHandler<Map>() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {
                progressDialog.dismiss();
                Toast.makeText(UpdateUserActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                setResult(UserInfo.TO_FRESH_USER_INFO);
                finish();
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                progressDialog.dismiss();
                Toast.makeText(UpdateUserActivity.this, "修改失败" + arg3.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
 }
